package com.atguigu.dwm.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.List;

public class JsonDemo {

    public static void main(String[] args) {
        JSONObject jsonObject = JSON.parseObject("{\"id\":123,\"name\":\"zhang3\"}");
        Customer customer = JSON.parseObject("{\"id\":123,\"name\":\"zhang3\"}", Customer.class);
        System.out.println("customer = " + JSON.toJSONString(customer) );

        String customersJson="[{\"id\":123,\"name\":\"zhang3\"},{\"id\":456,\"name\":\"li4\"}]";
        JSONArray jsonArray = JSON.parseArray(customersJson);
        List<Customer> customers = JSON.parseArray(customersJson, Customer.class);
        System.out.println("customers = " + JSON.toJSONString(customers) );
        List<JSONObject> jsonObjectList = JSON.parseArray(customersJson, JSONObject.class);

        System.out.println("jsonObjectList = " + JSON.toJSONString(jsonObjectList) );

    }

    @Data
   static public class Customer{
        private int id;
        private String name;
    }
}
